package com.example.dense_rack_server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.controller.Cellid;
import com.example.dense_rack_server.entity.*;
import com.example.dense_rack_server.mapper.ArchivesMapper;
import com.example.dense_rack_server.mapper.ArchivesSubnameMapper;
import com.example.dense_rack_server.mapper.SysParamsetMapper;
import com.example.dense_rack_server.security.dto.JwtUserDto;
import com.example.dense_rack_server.utils.DateFormat;
import com.example.dense_rack_server.utils.Netty.NettyServerHandler;
import com.example.dense_rack_server.utils.Netty.SendCmd;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张彬
 * @since 2025-11-13
 */
@Service
public class ArchivesSubnameService extends ServiceImpl<ArchivesSubnameMapper, ArchivesSubname> {

    @Resource
    public ArchivesSubnameMapper archivesSubnameMapper;
    @Resource
    public SysParamsetMapper sysParamsetMapper;
    @Resource
    public HostService hostService;
    @Resource
    public ArchivesMapper archivesMapper;
    @Resource
    public SendCmd sendCmd;



    /**
     * 列表查询
     **/
    public Map<String, Object> listTable(String sonFileName, String profile, String archivesId, int page, int limit) {
        Map<String, Object> paramsMap = new HashMap<>();
        // 手动处理分页
        int offset = (page - 1) * limit;
        paramsMap.put("page", offset);
        paramsMap.put("limit", limit);

        if (StrUtil.isNotEmpty(sonFileName)) {
            paramsMap.put("sonFileName", sonFileName);
        }
        if (StrUtil.isNotEmpty(profile)) {
            paramsMap.put("profile", profile);
        }
        if (StrUtil.isNotEmpty(archivesId)) {
            paramsMap.put("archivesId", archivesId);
        }
        List<Map<String, Object>> mapList = archivesSubnameMapper.archivesSubnameList(paramsMap);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", archivesSubnameMapper.archivesSubnameListCount(paramsMap));
        map.put("data", mapList);


//        IPage<Map<String, Object>> equipmentPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
//        QueryWrapper<Archives> wrapper = new QueryWrapper<>();
//        if (StrUtil.isNotEmpty(equipmentName)) {
//            wrapper.like("name", equipmentName);
//        }
//        if (StrUtil.isNotEmpty(areaId)) {
//            wrapper.eq("area_id", Convert.toInt(areaId));
//        }
//        equipmentPage = archiveMapper.selectMapsPage(equipmentPage, wrapper);
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);
//        map.put("msg", "数据请求成功");
//        map.put("count", equipmentPage.getTotal());
//        map.put("data", equipmentPage.getRecords());
        return map;
    }

    /**
     * 档案新增保存
     *
     * @author 张彬
     * @date 2024-08-30
     */
    public ResponseModel insertSave(JSONObject data) {
        ArchivesSubname archivesSubname = new ArchivesSubname();
        archivesSubname.setSonFileName(data.getStr("sonFileName"));
        archivesSubname.setCreateTime(DateFormat.getTime());
        if (data.getInt("drawingCount") != 0) {
            archivesSubname.setDrawingCount(data.getInt("drawingCount"));
        }else{
            archivesSubname.setDrawingCount(0);
        }
        archivesSubname.setArchivesId(data.getInt("archivesId"));
        archivesSubname.setProfile(data.getStr("profile"));
//        archivesSubname.setStatus(data.getInt("status"));
        int result = archivesSubnameMapper.insert(archivesSubname);
        if (result > 0) {
            return ResponseModel.success("新增图纸成功");
        } else {
            return ResponseModel.fail("新增图纸失败");
        }
    }

    /**
     * 档案预览编辑页面
     */
    public ModelAndView editView(int id) {
        ModelAndView modelAndView = new ModelAndView("archivesSubname/edit");
        ArchivesSubname archivesSubname = archivesSubnameMapper.selectById(id);
        modelAndView.addObject("archivesSubname", archivesSubname);
        return modelAndView;
    }

    /**
     * 编辑档案
     *
     * @author 张彬
     * @date 2024-08-30
     */
    public ResponseModel updateSave(JSONObject data) {
        ArchivesSubname archives = new ArchivesSubname();
        archives.setId(data.getInt("id"));
        archives.setSonFileName(data.getStr("sonFileName"));
        if (data.getInt("drawingCount") != 0) {
            archives.setDrawingCount(data.getInt("drawingCount"));
        }else{
            archives.setDrawingCount(0);
        }
        archives.setProfile(data.getStr("profile"));
        archives.setStatus(data.getInt("status"));
        int result = archivesSubnameMapper.updateById(archives);
        return result > 0 ? ResponseModel.success("编辑图纸成功") : ResponseModel.fail("编辑图纸失败");
    }

    /**
     * 档案查看详情
     *
     * @author 张彬
     * @date 2024-08-30
     */
    public ModelAndView archivesSubnameDetail(int id) {
        ModelAndView modelAndView = new ModelAndView("archivesSubname/info");
        ArchivesSubname archivesSubname = archivesSubnameMapper.selectById(id);
        modelAndView.addObject("archivesSubname", archivesSubname);
        return modelAndView;
    }

    /**
     * 图纸取出视图
     */
    public ModelAndView takeoutView(String archivesSubnameId) {
        ModelAndView modelAndView = new ModelAndView("archivesSubname/takeout");
        Map<String, Object> archivesMap = archivesSubnameMapper.archivesSubnameInfo(Integer.valueOf(archivesSubnameId));
        modelAndView.addObject("archives", archivesMap);

        SysParamset sysParamset = sysParamsetMapper.selectById(2);
        modelAndView.addObject("sysType", sysParamset.getParamValue());
        return modelAndView;
    }

    /**
     * 图纸归还视图
     */
    public ModelAndView returningView(String archivesSubnameId) {
        ModelAndView modelAndView = new ModelAndView("archivesSubname/returning");
        Map<String, Object> archivesMap = archivesSubnameMapper.archivesSubnameInfo(Integer.valueOf(archivesSubnameId));
        modelAndView.addObject("archives", archivesMap);

        SysParamset sysParamset = sysParamsetMapper.selectById(2);
        modelAndView.addObject("sysType", sysParamset.getParamValue());
        return modelAndView;
    }
}
